package com.sbhc.httpclient.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author fyy
 * @Description JSON解析工具类
 * @Date 2020/5/20 16:57
 */
public class JSONAnalysisUtil {

    private static Logger logger = LoggerFactory.getLogger(JSONAnalysisUtil.class);

    /**
     * @Author fyy
     * @Description 解析组织结构(仅用于当前组织结构解析)
     * @param organizeJsonStr 组织结构json字符串
     * @Date 2020/5/20 16:59
     */
    public static List<Map<String,String>> analysisOrganize(String organizeJsonStr){
        List<Map<String,String>> organizeList = new ArrayList<>();
        JSONArray jsonArray = JSONArray.parseArray(organizeJsonStr);
        for (Object obj : jsonArray) {
            JSONObject jsonObject = JSONObject.parseObject(obj.toString());
            if(!StringUtils.isEmpty(jsonObject.get("userList").toString())){
                JSONArray userJson = JSONArray.parseArray(jsonObject.get("userList").toString());
                for (Object user : userJson) {
                    JSONObject userObj = JSONObject.parseObject(user.toString());
                    Map<String,String> organizeMap = new HashMap<>();
                    organizeMap.put("username",userObj.get("name").toString());
                    organizeMap.put("phone",userObj.get("mobile").toString());
                    organizeMap.put("userid",userObj.get("userid").toString());
                    organizeMap.put("gqmc",jsonObject.get("name").toString());
                    organizeMap.put("gqid",jsonObject.get("id").toString());
                    organizeList.add(organizeMap);
                }
            }
        }
        return organizeList;
    }


}
